// components/showView/signUp/signUp.js
let utils = require('../../../utils/util.js')
let network = require("../../../utils/requestManager.js")
let app = getApp()
Component({
  /**
   * 组件的初始数据
   */
  data: {
    winHeight:"",
    shop_banner:"",
    shop_logo: "",
    shop_name: "",
    shop_address:"",
    no_activity:true,
    activitys:[],
    activity_time:{},
    signUp_data: {
      title: ""
    },
  },

  /**
   * 开启多组件支持
   * **/
  options:{
    addGlobalClass: true//启用全局样式
  },
  /**
   * 组件的属性列表
   */
  properties: {
    signUpData:{
      type: Object,//属性类型
      value: "",//初始值
      observer: function (newVal, oldVal, changePath) {//属性改变时执行函数
        this.setData({

        })
      }
    }
  },

  methods:{
    
  },
  /**
   * 倒计时组件调用函数
   * **/
  isFinished: function () {
    
  },
  /**
     * 生命周期函数，可以为函数，或一个在methods段中定义的方法名
     * **/
  lifetimes: {
    //组件附加上时
    attached: function () {
      
    },
    ready:function(){
      
    },
    moved: function () { },
    detached: function () { },
  },

  /**
  * 组件所在页面的生命周期函数
  * **/
  pageLifetimes: {
    /**
     * 组件显示时执行
     * 遍历活动
     * 如果活动开始
     * 获取对应组件，暂存
     * 转换UTC格式
     * 执行组件内倒计时
     * **/
    show: function () {
      console.log("微报名活动组件显示时执行更新")
      let that = this
      //设置视窗高
      wx.getSystemInfo({
        success: function (res) {
          that.setData({ winHeight: res.windowHeight })
        },
      })
      let shop_id = network.shopParams().shop_id
      console.log(app.globalData.owner)
      network.getRequest('apply/little/apply/product/list', { }, function (res) {
        console.log(res)
        that.setData({
          shop_address: res.shop_address,
          shop_name: res.shop_title,
          shop_logo: res.image_url,
          shop_banner: res.shop_image_url
        })
        let activity_arr = res.results;

        //no_activity判断是否有活动
        if (activity_arr.length !== 0) {
          that.setData({
            no_activity: false
          })
        }

        //循环遍历格式化日期
        // let activitys_arr = [];
        // let activity_item = {};

        for (var i = 0; i < activity_arr.length; i++) {
          activity_arr[i].end_time = utils.formatUTCDate(activity_arr[i].end_time)
          activity_arr[i].start_time = utils.formatUTCDate(activity_arr[i].start_time)
          // activity_arr[i].is_start = i == 0
          if (activity_arr[i].product_status == 3) {
            activity_arr[i].is_start = false;//如果活动已结束，对应的倒计时不渲染
          }else{
            activity_arr[i].is_start = true;//如果活动未结束，对应的倒计时渲染
          }
        }
        console.log(activity_arr)
        that.setData({
          activitys: activity_arr
        })
        startCountDown(that)
      })
      
      //triggerEvent:参数一为绑定组件的函数名，参数二为传递的数据
      this.triggerEvent("signCopmonent", { signUpData: this.data.signUpData.title })
    },
  },
  

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 点击跳转到活动详情页面
     * **/
    to_activity_detail(e){
      console.log(e)
      let data_id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/showView/signUp/weiActivity?activity_id=' + data_id,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }
  }
})

/**
 * 获取对应的组件
 * 格式化倒计时
 * 调用组件内倒计时函数
 * **/
function startCountDown(that){
  for (let i = 0; i < that.data.activitys.length;i++){
    let item = that.data.activitys[i]
    if(item.is_start){
      that.expireDate = that.selectComponent("#expireDate" + i);//获取对应组件
      let item_activity_status = item.product_status;//活动状态
      if (item_activity_status == 1) {//未开始
        let deadLine = utils.formatUTCDate(item.start_time);
        that.expireDate.startCountDown(deadLine);
      } else if (item_activity_status == 2) {//进行中
        let deadLine = utils.formatUTCDate(item.end_time);
        that.expireDate.startCountDown(deadLine);
      } else {//已结束
        let deadLine = "2010-01-10 00:00:00";
        that.expireDate.startCountDown(deadLine);
      }
    }
  

  }
}